import { Common } from '../../utils/common.js';
var common = new Common();
var url_datas = require('url_data.js')

const app = getApp();



Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_action_sheet: false,
    actions: [
      {
        name: '设为未完成将删除已上传的信息',
        className: 'title',
        disabled: true
      },
      {
        id: 1,
        name: '设为未完成',
        className: 'choose1'
      },
      {
        id: 3,
        name: "",
        className: 'choose3'
      },
      {
        id: 2,
        name: '取消',
        className: 'choose2'
      },
    ],
    show_icon: "none",
    operate: "完成",
    _top: '-88%',
    width: '311%',
    controller: true,
    show: false,
    url: false,
    item: url_datas.item,
    fin_url: url_datas.fin_url,
    unfin_url: url_datas.unfin_url,
  },

  //用户点击完成时
  complete: function (e) {
    this.navigate(e);
  },

  //用户点击未完成时
  no_complete: function (e) {
    var that = this;
    wx.hideTabBar({
      success: function () {
        that.setData({
          show_action_sheet: true,
          item_id: e.target.dataset.id,//保存点击的索引
        })
      },
    });
  },

  //点击取消时
  onClose() {
    this.setData({ show_action_sheet: false });
    wx.showTabBar({});
  },

  //删除或取消
  onSelect(event) {
    var id = event.detail.id;
    var that = this;
    if (id == 1) {
      var item_id = that.data.item_id;//对应的list的index索引
      var item = that.data.item;
      var data = '';

      if (item_id == 1)
        data = { url: 'ticket/del_all', type: '1' }
      else if (item_id == 2)
        data = { url: 'scheduling/del_all', type: '2' }
      else if(item_id==8){
            wx.removeStorage({
              key: 'luggage_list',
              success(res) {
                that.setData({
                  show_action_sheet: false,
                  'item[8].status': true,
                  'item[8].slide_width': 76,
                })
              }
            })
      }else
        data = { url: 'image/del_all', type: item[item_id].type}
  
      item[item_id]["url"] = that.data.unfin_url[item_id].url;//更改未完成后，跳转url变化
      item[item_id]["status"] = true;
      item[item_id]["slide_width"] = 76;
      var datas = {
        data:{type:data.type},
        type:'post',
        url:data.url
      }
      common.DataToCallBack(datas, data => {
        if (data.code == 201) {
          that.setData({
            item: item,
            show_action_sheet: false,
            slide_width: 76,
            show_icon: "none"
          })
        }
      })

    } else if (id == 2) {
      this.setData({
        show_action_sheet: false,
      })
    }
    
  },

  //点击跳转
  navigate:function(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  onShow:function(){
    var token = wx.getStorageSync('token');//获取缓存
    if (!token)
      app.CheckStatus = res => {
        if (res != '')
          this.TokenGetInfo();
      }
    else
      this.TokenGetInfo();
  },
  
  TokenGetInfo(){
    var that = this;
    var item = that.data.item;
    var datas = {
      type:'post',
      url:'scheduling/info'
    }
    common.ToCallBack(datas,data => {
      for (var i = 0; i < data.length; i++) {
        if (data[i] > 0){//大于0则已为已完成状态
          item[i]["status"] = false;
          item[i]["slide_width"] = 95;
          item[i]["url"] = that.data.fin_url[i].url;
        }
        else item[i]["url"] = that.data.unfin_url[i].url;
      }
      try {
        var value = wx.getStorageSync('luggage_list')
        console.log(value);
        if (value) {
          item[8]["status"] = false;
          item[8]["slide_width"] = 95;
        }
      } catch (e) {

      }
      that.setData({
        item: item,
      })
    })
    
  },
})